package com.weilaidb.administrator.myapplication;

import android.support.v7.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.icu.text.BreakIterator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;



public class SharePreferenceActivity extends AppCompatActivity {
    private EditText nameEditText = null;
    private EditText ageEditText = null;
    private Button saveButton = null;
    private Button resumeButton = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nameEditText = (EditText) findViewById(R.id.name);
        ageEditText = (EditText) findViewById(R.id.age);
        saveButton = (Button) findViewById(R.id.button);
        resumeButton = (Button) findViewById(R.id.resume);

        saveButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                SharedPreferences sharedPreferences = getSharedPreferences("test", Context.MODE_PRIVATE);
                Editor editor = sharedPreferences.edit();
                editor.putString("name", nameEditText.getText().toString());
                editor.putInt("age", new Integer(ageEditText.getText()
                        .toString()));
                editor.commit();
                Toast.makeText(SharePreferenceActivity.this, R.string.success, Toast.LENGTH_LONG)
                        .show();
            }
        });

        resumeButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                SharedPreferences sharedPreferences = getSharedPreferences("test", Context.MODE_PRIVATE);
                String name = sharedPreferences.getString("name", "");
                int age = sharedPreferences.getInt("age", 20);
                nameEditText.setText(name);
                ageEditText.setText(String.valueOf(age));
            }
        });
    }

}
